package com.ethink.order.facadeImpl;

import com.ethink.order.api.TocSaleReturnImportLogResultFacade;
import com.ethink.order.api.enums.TocSaleReturnImportLogResultReturnStatusEnum;
import com.ethink.order.api.info.TocSaleReturnImportLogDetailInfo;
import com.ethink.order.api.info.TocSaleReturnImportLogResultInfo;
import com.ethink.order.api.request.TocSaleReturnImportLogResultAddRequest;
import com.ethink.order.api.request.TocSaleReturnImportLogResultSettleRequest;
import com.ethink.order.service.TocSaleReturnImportLogDetailService;
import com.ethink.order.service.TocSaleReturnImportLogResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * TOC销售退单结果 Facade接口实现
 *
 * @author default
 * @date 2023-01-13
 */
@RestController
@Slf4j
@RequestMapping("order-service/facade/v1/tocSaleReturnImportLogResult")
public class TocSaleReturnImportLogResultFacadeImpl implements TocSaleReturnImportLogResultFacade {

    @Autowired
    private TocSaleReturnImportLogResultService tocSaleReturnImportLogResultService;

    @Autowired
    private TocSaleReturnImportLogDetailService tocSaleReturnImportLogDetailService;


    @Override
    public Boolean add(Long id) {
        List<TocSaleReturnImportLogDetailInfo> detailInfos = tocSaleReturnImportLogDetailService.queryDetailListByLogId(id);
        List<TocSaleReturnImportLogResultAddRequest> addRequestList = new ArrayList<>();
        Map<String, List<TocSaleReturnImportLogDetailInfo>> shopGroupBy = detailInfos.stream()
                .collect(Collectors.groupingBy(TocSaleReturnImportLogDetailInfo::getShopOrderNo));
        for (Map.Entry<String, List<TocSaleReturnImportLogDetailInfo>> stringListEntry : shopGroupBy.entrySet()) {
            TocSaleReturnImportLogDetailInfo detailInfo = stringListEntry.getValue().get(0);
            TocSaleReturnImportLogResultAddRequest addRequest = new TocSaleReturnImportLogResultAddRequest();
            addRequest.setLogId(detailInfo.getLogId());
            addRequest.setShopReturnOrderNo(detailInfo.getShopReturnOrderNo());
            addRequest.setShopOrderNo(detailInfo.getShopOrderNo());
            addRequest.setOrderNo(detailInfo.getOrderNo());
            addRequest.setReturnOrderNo(detailInfo.getReturnOrderNo());
            addRequest.setReturnStatus(TocSaleReturnImportLogResultReturnStatusEnum.S1.getIntValue());
            addRequestList.add(addRequest);
        }
        return tocSaleReturnImportLogResultService.batchInsert(addRequestList);
    }

    @Override
    public List<TocSaleReturnImportLogResultInfo> queryListByLogId(Long id) {
        return tocSaleReturnImportLogResultService.queryListByLogId(id);
    }

    @Override
    public void settleOrder(TocSaleReturnImportLogResultSettleRequest settleRequest) {
        tocSaleReturnImportLogResultService.settleOrder(settleRequest);
    }
}